<?php
/*----------------------------------------------------------------------
 * 菜单管理
 * Author: ming.nie
----------------------------------------------------------------------*/

namespace Admin\Controller;

use Think\Controller;
class MenuController extends AdminController {
    
    //菜单列表
    public function menuList(){
        $menuLogic = new \Common\Logic\MenuLogic();
        
        $params = array();
        $params['parent_id'] = 0;
        $menu_data_list = $menuLogic->getMultMenuList($params);
        $data_list = $menuLogic->getClearMultMenuList($menu_data_list, 0);
        
        //模板展示参数准备
        $yes_no_options = getYesOrNo();
        
        $this->assign(get_defined_vars());
        $this->display('menuList');
    }
    
    //添加菜单
    public function add(){
        
        $menuLogic = new \Common\Logic\MenuLogic();
        $userLogic = new \Common\Logic\UserLogic();
        
        $parent_id = I('request.parent_id', 0);
        
        if(IS_POST) {
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            
            $params = array();
            $params['name'] = I('post.name', '', 'strip_tags');
            $params['parent_id'] = $parent_id;
            $params['url'] = I('post.url', '', 'strip_tags');
            !$params['url'] && $params['url'] = '#';
            $params['is_show'] = I('post.is_show', '', 'strip_tags');
            $params['description'] = I('post.description', '', 'strip_tags');
            $params['user_id'] = $user_info['user_id'];
            $result = $menuLogic->add($params);
            if(1 == $result['status_code']) {
                $this->success($result['status_message'], U('Menu/menuList'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
        
        //查询上级用户组
        $parent = $menuLogic->find($parent_id);
        !$parent && $parent['name'] = '顶级';
        
        //模板展示参数准备
        $yes_no_options = getYesOrNo();
        
        $page_title = '添加菜单';
        $this->assign(get_defined_vars());
        $this->display('menu');
    }
    
    //编辑菜单
    public function edit(){
        $menuLogic = new \Common\Logic\MenuLogic();
        $userLogic = new \Common\Logic\UserLogic();
        
        $parent_id = I('request.parent_id', 0);
        $menu_id = I('request.menu_id');
        
        if(IS_POST) {
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            
            $params = array();
            $params['name'] = I('post.name', '', 'strip_tags');
            $params['parent_id'] = $parent_id;
            $params['url'] = I('post.url', '#', 'strip_tags');
            !$params['url'] && $params['url'] = '#';
            $params['is_show'] = I('post.is_show', '', 'strip_tags');
            $params['description'] = I('post.description', '', 'strip_tags');
            $params['user_id'] = $user_info['user_id'];;
            $params['menu_id'] = $menu_id;
            $result = $menuLogic->edit($params);
            if(1 == $result['status_code']) {
                $this->success($result['status_message'], U('Menu/menuList'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
        
        //查询上级用户组
        $parent = $menuLogic->find($parent_id);
        !$parent && $parent['name'] = '顶级';
        
        //查询当前用户组
        $model = $menuLogic->find($menu_id);
        
        //模板展示参数准备
        $yes_no_options = getYesOrNo();
        
        $page_title = '编辑菜单';
        $this->assign(get_defined_vars());
        $this->display('menu');
    }
    
    //删除菜单
    public function delete(){
        $menuLogic = new \Common\Logic\MenuLogic();
        $userLogic = new \Common\Logic\UserLogic();
        
        //当前用户信息
        $user_info = $userLogic->getCurrentUserInfo();
        
        $params = array();
        $params['menu_id'] = I('request.menu_id');
        $params['user_id'] = $user_info['user_id'];
        $result = $menuLogic->delete($params);
        if(1 == $result['status_code']) {
            $this->success($result['status_message'], C('REFERER'), 3);
        } else {
            $this->error($result['status_message'], C('REFERER'), 3);
        }
    }
    
    //菜单排序
    public function sort(){
        $menuLogic = new \Common\Logic\MenuLogic();
        $params = array();
		$params['sort_ids'] = I('request.sort_ids');
		$params['sort_values'] = I('request.sort_values');
		$result = $menuLogic->sort($params);
		$this->success($result['status_message'], C('REFERER_URL'));
        if(1 == $result['status_code']) {
            $this->success($result['status_message'], C('REFERER'), 3);
        } else {
            $this->error($result['status_message'], C('REFERER'), 3);
        }
    }
}